PWD:=$(shell pwd)

OUTPUT_DIR:=$(PREFIX)
LIB_NAME:=libcjson
TEST_EXE:=cjson_test

ifeq ($(LINKTYPE),shared)
CFLAGS := -fPIC
TARGET := $(LIB_NAME).so
else
TARGET := $(LIB_NAME).a
endif

ifeq ($(HOST),)
CROSS_COMPILE:=
else
CROSS_COMPILE:=$(HOST)-
endif

CC:=$(CROSS_COMPILE)gcc
AR:=$(CROSS_COMPILE)ar

all: ${TARGET}

${LIB_NAME}.a:	cJSON.o
	$(AR) -r $(LIB_NAME).a cJSON.o
	
${LIB_NAME}.so:	cJSON.o
	$(CC) -fPIC -shared -o $(LIB_NAME).so cJSON.o

cJSON.o:
	$(CC) $(CFLAGS) cJSON.c -c -o cJSON.o 

test:
	$(CC) *.c -o $(TEST_EXE) -lm
	
install:
	-mkdir -p $(OUTPUT_DIR)/lib
	-mkdir -p $(OUTPUT_DIR)/bin
	-mkdir -p $(OUTPUT_DIR)/include
	cp $(TARGET) $(OUTPUT_DIR)/lib
	cp cJSON.h $(OUTPUT_DIR)/include/
	cp $(TEST_EXE) $(OUTPUT_DIR)/bin
	
.PHONY: clean
clean:
	@rm -rf *.o *.a *.so $(TEST_EXE)